<?php
if(!defined('prefix_db')){define('prefix_db','');}
/**
 * data_json_component
 *
 * @author tuadmin
 * @copyright 2011
 * @version 1.1
 * @access public
 */
class data_json_component
{
	private $data = array();
	private $archivo;
	public $author = "";
	public $version = "";
	public $description = "";
	public $ico ="";
	public $path = "";
	public $name = "";
	public $methods = array();
	public $constants;
	public $permissions;
	public $temporal_directory = '';
	public $sql;
	public $folders;
	private $exist_file = false;
	public static function newInstance($file)
	{
		return new self($file);
	}
	public function __construct($file)
	{
		$this->archivo = $file;
		$this->methods = new ArrayIterator();
		$this->permissions = new ArrayIterator();
		if(file_exists($file))
		{
			$datos = json_decode(file_get_contents($file));
			//var_dump($datos);
			$this->author = isset($datos->author)?$datos->author:'';
			$this->version = isset($datos->version)?$datos->version:'';
			$this->description = isset($datos->description)?$datos->description:'';
			$this->ico = isset($datos->ico)?$datos->ico:'';
			$this->path = isset($datos->path)?$datos->path:'';
			$this->name = isset($datos->name)?$datos->name:'';

			$this->exist_file = true;
			$this->methods = isset($datos->methods)?new ArrayIterator($datos->methods): new ArrayIterator();
			$this->permissions = isset($datos->permissions)?$datos->permissions:array();
			$this->constants = isset($datos->constants)?$datos->constants:array();
			$this->temporal_directory = isset($datos->temporal_directory)?$datos->temporal_directory:'';
			if(isset($datos->sql) && is_object($datos->sql) && isset($datos->sql->install))
			{
				$this->sql =(object)array
					('install' =>  isset($datos->sql->install)?base64_decode($datos->sql->install):false
					,'uninstall' => isset($datos->sql->uninstall)?base64_decode($datos->sql->uninstall):false);
				//$this->sql->uninstall = isset($datos->sql->uninstall)?base64_decode($datos->sql->uninstall):false;
				if(is_string($this->sql->install) && strpos($this->sql->install ,'@PREFIX@' ) !== false)
				{
					$this->sql->install = strtr($this->sql->install ,array('@PREFIX@' => prefix_db));
				}
				if(is_string($this->sql->uninstall) && strpos($this->sql->uninstall ,'@PREFIX@' ) !== false)
				{
					$this->sql->uninstall = strtr($this->sql->uninstall ,array('@PREFIX@' => prefix_db));
				}
			}
			else
			{
				$this->sql = (object)array('install' => false, 'uninstall' => false);
			}
			if(isset($datos->folders) )
			{
				$array = array();
				if(isset($datos->folders->temp))
				{
					$array["temp"] = $datos->folders->temp;
				}
				if(isset($datos->folders->configuration))
				{
					$array["configuration"] = $datos->folders->configuration;
				}
				if(isset($datos->folders->media))
				{
					$array["media"] = $datos->folders->media;
				}
				$this->folders = (object) $array;
			}
			unset ($datos);
		}
		else
		{
			$this->methods = new ArrayIterator();
			$this->permissions = new stdClass();
			$this->constants = new ArrayIterator(array(
			"code" => '',
			"constant" => new ArrayIterator(array("key" => "value"))
			));

		}
	}
	public function getMethod($position = false)
	{
		if(false === $position)
		{
			return $this->methods;
		}
		else
		{
			return $this->methods[$position];
		}
	}
	public function appendMethod($name)
	{
		$this->methods->append($name);
		return $this;
	}
	public function getPerm($name)
	{
		if(isset($this->permissions->{$name}))
		{
			return $this->permissions->{$name};
		}
		else
		{
			return false;
		}
	}
	public function setPerm($name,$value = 0,$description = '')
	{
		$this->permissions->{$name} = (object) array(
		"description" => $description,
		"default_value" => $value
		);
		return $this;
	}
	public function createDataJson()
	{
		$this->methods = $this->methods->getArrayCopy();
		echo json_encode($this);
	}
	public function setAttribute($key,$value)
	{
		$this->{$key} = $value;
		return $this;
	}
}